package com.jamcodingnm.apps.abqevents.rss;

import java.net.URL;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;


public class TestParser {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		getFeed("http://data.cabq.gov/community/events/RSS");
	}

	private static void getFeed(String urlToRssFeed) {
		try {
			// setup the url
			URL url = new URL(urlToRssFeed);

			// create the factory
			SAXParserFactory factory = SAXParserFactory.newInstance();
			// create a parser
			SAXParser parser = factory.newSAXParser();

			// create the reader (scanner)
			XMLReader xmlreader = parser.getXMLReader();
			// instantiate our handler
			RSSHandler theRssHandler = new RSSHandler();
			// assign our handler
			xmlreader.setContentHandler(theRssHandler);
			// get our data through the url class
			InputSource is = new InputSource(url.openStream());
			// perform the synchronous parse
			xmlreader.parse(is);
			// get the results - should be a fully populated RSSFeed instance,
			// or null on error

			for (RSSItem rssItem : theRssHandler.getFeed().getItemlist()) {
				System.err.println("Title:       " + rssItem.getTitle() + "\n"
								+ "Description: " + rssItem.getDescription() + "\n"
								+ "Category:    " + rssItem.getCategory() + "\n"
								+ "Link:        " + rssItem.getLink() + "\n"
								+ "Date:    	" + rssItem.getPubDate() + "\n"
								+ "Month:    	" + rssItem.getMonth() + "\n"
								+ "Day:     	" + rssItem.getDay()+ "\n"
								+ "Year:    	" + rssItem.getYear() + "\n"
								+ "StringDate:	" + rssItem.getStringDate() + "\n"
								+ "Image URL:   " + rssItem.getImageURL());
				for (String detail : rssItem.getDetail()) {
					System.err.println("Detail:      " + detail);
				}
				System.err.println("\n");

			}
			return;
		} catch (Exception ee) {
			ee.printStackTrace();
			return;
		}
	}
}
